Skip to content

Preserve quota accounting when a usage write fails - #52

Closed
compnew2006 wants to merge 1 commit into
UserJoo9:mainfrom
compnew2006:codex/qm-02-usage-retry
Closed

compnew2006 wants to merge 1 commit into
UserJoo9:mainfrom
compnew2006:codex/qm-02-usage-retry

Conversation

@compnew2006

Copy link
Copy Markdown
Contributor

Problem

The maintenance tick advances the packet counter baseline when it reads nftables. If the subsequent SQLite write fails, the next tick reads only the newer delta. A synthetic failure before saving 1,000 bytes, followed by 300 additional bytes, recorded only 300. This can understate a user's quota consumption and delay enforcement. Retrying the write without identifying the batch can instead count it twice if the original commit succeeded but its acknowledgement was lost.

Changes

  • Keep one pending usage batch and retry it before draining the next set of counters. Its identity, device attribution, capture date and free-mode state travel with the retry.
  • Store a receipt and apply usage and free-mode rows in the same SQLite transaction, using an isolated connection for a file-backed database. Prune old receipts using an indexed timestamp.
  • Preserve usage of a removed device when its owner still exists. Skip records whose device is unknown or whose account has already been purged.

Before and after

Scenario Before After
A write fails before committing the 1,000-byte batch The baseline advances and the next tick stores just the new 300 bytes The next tick retries the original batch, then stores the new delta, for 1,300 bytes
SQLite commits, but the caller sees an error An unconditional retry could duplicate the batch The receipt makes retry of the same batch a no-op
A device is removed while a batch is pending Recomputing attribution from live devices can omit previously captured usage A retained owner gets the captured usage, and a purged account is left alone

The new receipt table and index are additive. A pending batch is held in process memory. This change handles write failures and uncertain commit acknowledgements while the gateway process stays up. It does not introduce a crash-recovery spool. Rolling back the application stops using the additive receipt table and leaves existing quota data intact.

Verification

The isolated Gateway tests cover failure before commit and a commit followed by an error. Database tests cover duplicate receipts, free mode, ownership after device removal and in-memory database behavior. No live packet counters or gateway data were used.

UserJoo9 added a commit that referenced this pull request Nov 24, 2026
…s (idea from #52)

Keep counted usage when a database write fails (idea from #52)
@UserJoo9

Copy link
Copy Markdown
Owner

Thanks a lot for finding this — it's a real gap. We went with a smaller fix in #66 (all-or-nothing write, keep the failed batch in memory and retry it next tick, the tick keeps going), so no second connection or receipts table is needed. You're credited in the CHANGELOG. Closing this one in favour of #66.

@UserJoo9 UserJoo9 closed this Nov 24, 2026
kirolos-esmat pushed a commit to kirolos-esmat/QuotaManager that referenced this pull request Nov 24, 2026
…oing

The kernel counters are drained before the write, so a failed write lost
those bytes and aborted the tick before blocks were evaluated. The batch is
now all-or-nothing (rollback on error), kept in memory, merged into the next
tick's batch and retried. Simpler take on UserJoo9#52 by @compnew2006 (one shared
connection, no receipts table). Regression test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants